package com.caregiver.watch.business.care.service.strategy;

import com.caregiver.watch.business.care.dao.HealthAlertConfigDao;
import com.caregiver.watch.business.care.enums.HealthAlertTypeEnum;
import com.caregiver.watch.business.care.model.bo.AlertMessageBo;
import com.caregiver.watch.business.care.model.po.HealthAlertConfig;
import com.caregiver.watch.business.care.model.po.UserAlertRecord;
import com.caregiver.watch.business.care.service.UserAlertRecordService;
import com.caregiver.watch.business.user.model.po.UserCareRecipient;
import com.caregiver.watch.business.user.service.UserDeviceService;
import com.caregiver.watch.thirdparty.model.dto.PushCustomMessageDTO;
import com.caregiver.watch.thirdparty.model.dto.PushNotificationDTO;
import com.caregiver.watch.thirdparty.service.JPushService;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ONE;
import static com.caregiver.watch.common.constant.NumberConstants.ONE_LONG;
import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 抽象的预警策略
 * @Author 疆戟
 * @Date 2025/10/26 12:50
 * @Version 1.0
 */
@Slf4j
public abstract class AbstractHealthWarningStrategy<T> implements HealthWarningStrategy<T> {

    private final UserDeviceService userDeviceService;
    private final HealthAlertConfigDao healthAlertConfigDao;
    private final UserAlertRecordService userAlertRecordService;
    private final JPushService jPushService;

    public AbstractHealthWarningStrategy(UserDeviceService userDeviceService,
                                         HealthAlertConfigDao healthAlertConfigDao,
                                         UserAlertRecordService userAlertRecordService,
                                         JPushService jPushService) {
        this.userDeviceService = userDeviceService;
        this.healthAlertConfigDao = healthAlertConfigDao;
        this.userAlertRecordService = userAlertRecordService;
        this.jPushService = jPushService;
    }

    protected HealthAlertConfig getConfig() {
        return healthAlertConfigDao.selectByAlertType(getAlertType());
    }

    /**
     * 查询设备详情
     * @param userId
     * @param deviceUuid
     * @return
     */
    protected UserCareRecipient queryContactDeviceDetail(Long userId, String deviceUuid) {
        return userDeviceService.queryContactDeviceDetail(userId, deviceUuid);
    }

    /**
     * 发送推送
     * @param userId
     * @param recipientId
     * @param alertMessage
     * @return
     */
    protected Boolean sendPush(Long userId, Long recipientId, HealthAlertTypeEnum alertType, LocalDateTime alertTime, AlertMessageBo alertMessage) {
        try {
            PushNotificationDTO pushCustomMessageDTO = new PushNotificationDTO();
            // 查询推送ID
            return jPushService.sendNotification(pushCustomMessageDTO);
        } catch (Exception e) {
            log.error("发送推送失败", e);
            return false;
        }
    }

    /**
     * 保存告警记录
     * @param userId
     * @param recipientId
     * @param alertMessage
     * @return
     */
    protected Boolean saveAlertRecord(Long userId, Long recipientId, HealthAlertTypeEnum alertType, LocalDateTime alertTime, String alertMessage) {
        try {
            UserAlertRecord userAlertRecord = new UserAlertRecord();
            userAlertRecord.setUserId(userId);
            userAlertRecord.setRecipientId(recipientId);
            userAlertRecord.setAlertType(alertType.name());
            userAlertRecord.setAlertMessage(alertMessage);
            userAlertRecord.setIsRead(ZERO);
            userAlertRecord.setAlertTime(alertTime);
            return userAlertRecordService.insert(userAlertRecord);
        } catch (Exception e) {
            log.error("保存告警记录失败", e);
            return false;
        }
    }
}
